package jh.com.jhappbf.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;

import jh.com.jhappbf.database.SQLiteDataBaseUtil;

public class ClearCache {

    /**
     * 删除应用内部缓存
     */
    public static void clearInternalCache(Context context) {
        deleteFilesByDirectory(context.getCacheDir());
    }

    /**
     * 删除数据库
     *
     * @param context
     * @return
     */
    public static boolean clearDataBase(Context context) {
        SQLiteDataBaseUtil.closeData();
        boolean b = context.deleteDatabase("data");
        return b;
    }

    /**
     * 清除配置文件
     */
    public static void clearSharedPreference(Context context) {
        deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs"));
    }

    /**
     * 删除内部数据
     */
    public static void clearInternalFiles(Context context) {
        deleteFilesByDirectory(context.getFilesDir());
    }

    /**
     * 清除外部的缓存中的数据
     */
    public static void clearExternalCache(Context context) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            deleteFilesByDirectory(context.getExternalCacheDir());
        }
    }

    /**
     * 清除自定义缓存
     *
     *
     */
    public static void clearCustomCache() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/jinHeng/img";
            File file = new File(path);
            deleteFilesByDirectory(file);
        }
    }

    public static void deleteFilesByDirectory(File f) {
        if (f != null && f.exists() && f.isDirectory()) {
            for (File item : f.listFiles()) {
                item.delete();
            }
        }
    }

    /**
     * 清除应用缓存
     */
    public static void applicationCleanCache(Context context) {
        clearInternalCache(context);
        clearDataBase(context);
        clearInternalFiles(context);
        clearSharedPreference(context);
        clearExternalCache(context);
        clearCustomCache();
    }
}
